<template>
    <el-dialog :title="title" :visible.sync="dialogVisible" width="700px" append-to-body>
        <el-form ref="form" :model="model" :rules="rules" inline label-width="110px" label-suffix=":" class="form-class" v-loading="loading">
            <el-form-item label="店铺" prop="shop_id">
                <el-select v-model="model.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                    <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                </el-select>
            </el-form-item>
            <el-form-item label="标签" prop="tag">
                <!-- <el-input v-model.trim="model.tag" placeholder="ALL表示所有标签" clearable></el-input> -->
                <el-select v-model="model.tag" placeholder="ALL表示所有标签" filterable clearable size="small" style="width: 240px">
                    <el-option v-for="(dict, index) in tagList" :key="index" :label="dict.label" :value="dict.label" />
                </el-select>
            </el-form-item>
            <!-- <el-form-item label="开始时间" prop="date_zone_start">
                <el-date-picker v-model="model.date_zone_start" type="date" placeholder="请选择开始时间" clearable> </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="date_zone_end">
                <el-date-picker v-model="model.date_zone_end" type="date" placeholder="请选择结束时间" clearable> </el-date-picker>
            </el-form-item> -->
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" :loading="save" :disabled="save" @click="submit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
        </div>
    </el-dialog>
  </template>
  
  <script>
  import { addDelTag, getShopList, getTagList } from '@/api/customer'
  import { parseTime } from '@/utils/quickadmin'
  export default {
    name: 'EditDialog',
    props: {
        value: {
            type: Boolean,
            default: false,
            required: true,
        },
        params: {
            type: Object,
            default: () => {},
        },
    },
    data() {
        return {
            loading: false,
            save: false,
            model: {
                shop_id: '',
                tag: 'ALL',
                // date_zone_start: '',
                // date_zone_end: ''
            },
            rules: {
                shop_id: [{ required: true, message: '店铺不能为空', trigger: ['blur', 'change'] }],
                tag: [{ required: true, message: '标签不能为空', trigger: ['blur', 'change'] }],
                date_zone_start: [{ required: true, message: '开始时间不能为空', trigger: ['blur', 'change'] }],
                date_zone_end: [{ required: true, message: '结束时间不能为空', trigger: ['blur', 'change'] }],
            },
            shopList: [],
            tagList: [],
        }
    },
    computed: {
        title() {
            return `删除标签`
        },
        dialogVisible: {
            get() {
                return this.value
            },
            set(val) {
                this.$emit('input', val)
            },
        },
    },
    watch: {
        params: {
            handler(val) {
                if (val.id) {
                    this.model = Object.assign({}, this.model, val)
                }
            },
            immediate: true,
            deep: true,
        },
    },
    async created() {
          this.loading = true
          try {
              const result = await getShopList()
              this.shopList = result?.data ?? []
              const tagData = await getTagList()
              this.tagList = tagData?.data ?? []
          } catch (error) {
              console.log(error)
          }
          this.loading = false
      },
    methods: {
        submit() {
            this.$refs.form.validate(valid => {
                if (!valid) {
                    return
                }
  
                const params = { ...this.model }
                if(params.date_zone_start) {
                    params.date_zone_start = parseTime(params.date_zone_start, 'y-m-d')
                }
                if(params.date_zone_end) {
                    params.date_zone_end = parseTime(params.date_zone_end, 'y-m-d')
                }
                if (this.save) {
                    return
                }
                this.save = true
                addDelTag(params)
                    .then(res => {
                        this.$message.success(res.msg)
                        this.dialogVisible = false
                        this.$emit('success')
                    })
                    .finally(() => {
                        this.save = false
                    })
            })
        },
        cancel() {
            this.dialogVisible = false
        },
    },
  }
  </script>
  
  <style>
  .form-class.el-form .el-form-item {
    width: 100%;
    margin-right: 0;
  }
  .form-class.el-form .el-form-item .el-input {
    width: 400px;
  }
  </style>
  